<?php

class admin_AuthentificationController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->layout()->disableLayout();
        $this->_helper->layout()->setLayout("authlayout");
    }

    public function indexAction()
    {
        // action body
    	// action body 
    }

    public function loginAction()
    {
        // action body
    	
    	
    	$auth= new Application_Form_Authentification();
    	$this->view->form = $auth;
    	 
    	$this->view->FormulaireAuthentification = $auth;
    	if ($this->_request->isPost()) {
    		 
    		$formData = $this->_request->getPost();
    		 
    		if ($auth->isValid($formData)) {
    			
    			$DbAdapter=Zend_Db_Table::getDefaultAdapter();
    				
    			$authAdapter = new Zend_Auth_Adapter_DbTable($DbAdapter);
    			
    			$authAdapter->setTableName('users');
    			$authAdapter->setIdentityColumn('login');
    			$authAdapter->setCredentialColumn('motpasse');
    			//$authAdapter->setCredentialTreatment('MD5(?)');
    			
    			// Set the input credential values to authenticate against
    			$authAdapter->setIdentity($formData["login"]);
    			$authAdapter->setCredential(($formData["motpass"]));
    			 
    			$auth = Zend_Auth::getInstance();
    			 
    			/*************************************/
    			
    			
    			try {
    				 
    				$result = $auth->authenticate ( $authAdapter );
    				
    				switch ($result->getCode ()) {
    					case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND :
    						$message = "{success: false, msg: {text: 'Utilisateur non trouvé !', code: '2'}}";
    					break;
    					case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID :
    						$message = "{success: false, msg: {text: 'mot de passe incorect', code: '3'}}";
    					break;
    					case Zend_Auth_Result::SUCCESS :
    						$data = $authAdapter->getResultRowObject(null, 'password');
    						$auth->getStorage()->write($data);
    					if ($data->idtypeusers=='premier responsable') {
    						//$auth->getStorage ()->write ( $data );
    						//$message = "{success:true, link: '" . $this->_request->getBaseUrl () . "'}";
    						
    						
    						$this->_helper->redirector("index","Gusers","Gcentrale");
    						
    					} else {
    						//$result->auth->clearIdentity ();
    						//$message = "{success:false, msg: {text: 'non autoriser, code:'2'}}";
    						
    						$this->_helper->redirector("index","Gusers","Gcentrale");
    					}
    					break;
    				}
    			} catch ( Zend_Db_Adapter_Exception $e ) {
    				$message = "{success:false, msg: {text: 'Erreur de conexion, " . $e->getMessage () . "', code:'6'}}";
    			} catch ( Zend_Exception $e ) {
    				$message = "{success:false, msg: {text: 'Erreur Systeme, " . $e->getMessage () . "', code:'6'}}";
    			}
    			
    			echo $message;die();
    			
    			/****************************************/
    			
    			
    			
    			
    		}
    	}
    	$this->view->title="Authentification";
    	$this->view->form=$auth;
    }

    public function logoutAction()
    {
        // action body
    	$this->_helper->layout()->disableLayout();
     	$this->_helper->viewRenderer->setNoRender(true);
    	
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_helper->redirector('login','authentification','admin');
    }


}





